package com.jlboot.app.admin.controller.sys;

import com.jlboot.core.annotation.JlActivity;
import com.jlboot.core.base.PageSearch;
import com.jlboot.app.admin.ui.plugins.layui.LayuiData;
import com.jlboot.core.json.JsonResult;
import com.jlboot.core.model.DataMap;
import com.jlboot.app.admin.base.BaseController;
import com.jlboot.core.cache.SysTypeCache;
import com.jlboot.core.form.FormData;
import com.jlboot.app.admin.interceptor.Admins;
import com.jlboot.core.plugins.UploadUtil;
import com.jlboot.core.util.CrudUtil;
import com.jlboot.model.po.sys.SysPost;
import com.jlboot.app.admin.validate.SysPostValidator;
import com.jfinal.aop.Before;
import com.jlboot.core.plugins.tree.TreeNode;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by wuyang(51594) on 2022-11-04 14:41:11.
 */
public class SysPostController extends BaseController {

    @Admins(roles = "login", name = "系统岗位默认", log = false)
    public void index() {
        renderText("系统岗位默认");
    }


    /**
     * 系统岗位列表显示页面
     */
    @JlActivity( name = "系统岗位列表List", log = false)
    public void SysPostList() {
        FormData fd = FormData.n();
        TreeNode tree = TreeNode.newNode("page1").setTitle("测试分类").setSpread(true).setTurn(false);
        tree = SysTypeCache.n().getTypeDetailTree(tree, "test_type");
        fd.addData("tree", toJsonStr(TreeNode.newNodeList(tree)));
        fd.addData("def_id", tree.getDatas().get("def_id"));
        fd.addData("def_title", tree.getDatas().get("def_title"));

        setAttr("form", fd);
        render("SysPostList.html");
    }

    /**
     * 系统岗位列表数据查询API
     * fd.addSelect("", SysDataCache.getFormSelect(""));
     */
    @JlActivity( name = "系统岗位列表数据查询Api", log = false)
    public void SysPostListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_post");

        ps.orderby(getRequest(), "id desc");
        Page<SysPost> pages = ps.paginate(SysPost.dao);
        List<SysPost> datalist = pages.getList();
        for (SysPost post : datalist) {

        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加系统岗位Page
     */
    @JlActivity( name = "添加系统岗位Page", log = false)
    public void AddSysPost() {
        FormData fd = FormData.n().setActions("ADD");
        setAttr("post", new SysPost());
        setAttr("form", fd);
        render("ModifySysPost.html");
    }

    /**
     * 修改系统岗位Page
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @JlActivity( name = "修改系统岗位Page", log = false)
    public void EditSysPost() {
        String id = getPara("id");
        SysPost o = SysPost.dao.findById(id);
        FormData fd = FormData.n().setActions("EDIT");
        setAttr("post", new SysPost());
        if (o != null) {

        }
        setAttr("form", fd);
        setAttr("post", o);
        render("ModifySysPost.html");
    }

    /**
     * 修改系统岗位Api
     */
    @Before(SysPostValidator.class)
    @JlActivity( name = "修改系统岗位Api", log = true)
    public void ModifySysPostApi() {
//取得数据
        DataMap data = getDatas();
        String id = data.getStr("id");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysPost.dao.findById(id)
                    .set("post_name", data.getStr("post_name"))
                    .set("sort", data.getInt("sort"))
                    .set("post_desc", data.getStr("post_desc"))

                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysPost()
                    .set("id",SysPost.getNextId())
                    .set("post_name", data.getStr("post_name"))
                    .set("sort", data.getInt("sort"))
                    .set("post_desc", data.getStr("post_desc"))

                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改系统岗位字段Api
     * demo:
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @JlActivity( name = "修改系统岗位字段Api", log = true)
    public void ModifySysPostFieldApi() {
        String id = getPara("key_id");
        String field = getPara("field");
        String value = getPara("values");
        SysPost o = SysPost.dao.findById(id);
        if (o != null) {
            boolean status = o.set(field, value).update();
            if(status){
                renderJson(JsonResult.ok());
            }else{
                renderJson(JsonResult.fail());
            }
        } else {
            renderJson(JsonResult.okWithMsg("修改成功"));
        }
    }


    /**
     * 删除系统岗位Api
     */
    @JlActivity( name = "删除系统岗位Api", log = true)
    public void DelSysPostApi() {
        String id = getPara("id");
        SysPost post = SysPost.dao.findById(id);
        if (post == null) {
            renderJson(JsonResult.fail( "记录不存在"));
        } else if (post.delete()) {
            renderJson(JsonResult.okWithMsg("删除成功"));
        } else {
            renderJson(JsonResult.fail( "删除失败"));
        }
///////////////////////////////////
    }


    /**
     * 批量删除系统岗位Api
     */
    @JlActivity( name = "批量删除系统岗位Api", log = true)
    public void DelMoreSysPostApi() {
        Integer[] result= CrudUtil.DelMoreModel(SysPost.table,getParaStr("keys", "").split(","));
        renderJson(JsonResult.okWithMsg("删除成功，【共删除成功 " + result[0] + ",失败 " + result[1] + "】"));
    }

    /**
     * 系统岗位图片上传接口
     */
    @JlActivity( name = "系统岗位图片上传Api", log = false)
    public void UploadFile() {
        renderJson(UploadUtil.n().upload(getRequest(), "/jlboot", true, true));
    }


    /**
     * 改变系统岗位字段状态Api(0 or 1)
     */
    @JlActivity( name = "改变系统岗位字段状态Api", log = true)
    public void ChangeSysPostTagApi() {
        String keyid = getParaStr("key", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysPost.dao, keyid, field, msg));
    }
}